﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SMTC.Model.Model;
using SMTC.BO.BaseInfo;
using SMTC.WebCommon.Common;

public partial class BaseInfoManagement_UpLoadMateril : BasePage
{
    ProductBaseInfoServices _service = new ProductBaseInfoServices();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        if (ddl_Operation.SelectedValue == "1")
        {
            this.ProductHalf();
        }
        else
        {
            this.ProgramName();
        }
    }

    private void ProductHalf()
    {
        int ExcelTableIndex = 0; if (!string.IsNullOrEmpty(txt_ExcelTableIndex.Text.Trim())) ExcelTableIndex = ConvertDateType.IntParse(txt_ExcelTableIndex.Text.Trim()) - 1;
        int ProductCol = 0; if (!string.IsNullOrEmpty(txt_ProductCol.Text.Trim())) ProductCol = ConvertDateType.IntParse(txt_ProductCol.Text.Trim()) - 1;
        int UnitCountCol = 1; if (!string.IsNullOrEmpty(txt_HalfProductCol.Text.Trim())) UnitCountCol = ConvertDateType.IntParse(txt_HalfProductCol.Text.Trim()) - 1;
        int StartRow = 0; if (!string.IsNullOrEmpty(txt_StartRow.Text.Trim())) StartRow = ConvertDateType.IntParse(txt_StartRow.Text.Trim());
        int EndRow = 0; if (!string.IsNullOrEmpty(txt_EndRow.Text.Trim())) EndRow = ConvertDateType.IntParse(txt_EndRow.Text.Trim());

        DataSet ds = UP_PlanAnalyze.AnalyzeExcelToDS();
        var result = (from row in ds.Tables[ExcelTableIndex].AsEnumerable()
                      select new SMT_ProductBaseInfoModel
                      {                          
                          ProductCode = row.Field<string>(ProductCol),
                          UnitCount = int.Parse(row.Field<string>(UnitCountCol)),
                          Para = float.Parse("1.0"),
                          Operation = this.UserName,
                          OperationTime = DateTime.Now,
                          isTemp = 0
                      }).Distinct();

        List<SMT_ProductBaseInfoModel> list = result.Cast<SMT_ProductBaseInfoModel>().ToList<SMT_ProductBaseInfoModel>();

        if (StartRow != 0)
        {
            list = list.Skip(StartRow - 1).Cast<SMT_ProductBaseInfoModel>().ToList<SMT_ProductBaseInfoModel>();
        }

        if (EndRow != 0)
        {
            list = list.Take(EndRow - StartRow + 1).Cast<SMT_ProductBaseInfoModel>().ToList<SMT_ProductBaseInfoModel>();
        }
        try
        {
            foreach (var item in list)
            {
                _service.InsertOrUpdate(item);
            }
            ShowMessage("success");
        }
        catch
        {
            ShowMessage("fail");
        }
    }

    private void ProgramName()
    {
        int ExcelTableIndex = 0; if (!string.IsNullOrEmpty(txt_ExcelTableIndex.Text.Trim())) ExcelTableIndex = ConvertDateType.IntParse(txt_ExcelTableIndex.Text.Trim()) - 1;
        int HalfProductCol = 0; if (!string.IsNullOrEmpty(txt_ProductCol.Text.Trim())) HalfProductCol = ConvertDateType.IntParse(txt_ProductCol.Text.Trim()) - 1;
        int ProgramNameCol = 1; if (!string.IsNullOrEmpty(txt_HalfProductCol.Text.Trim())) ProgramNameCol = ConvertDateType.IntParse(txt_HalfProductCol.Text.Trim()) - 1;
        int StartRow = 0; if (!string.IsNullOrEmpty(txt_StartRow.Text.Trim())) StartRow = ConvertDateType.IntParse(txt_StartRow.Text.Trim());
        int EndRow = 0; if (!string.IsNullOrEmpty(txt_EndRow.Text.Trim())) EndRow = ConvertDateType.IntParse(txt_EndRow.Text.Trim());

        DataSet ds = UP_PlanAnalyze.AnalyzeExcelToDS();
        var result = (from row in ds.Tables[0].AsEnumerable()
                      select new SMT_ProductBaseInfoModel
                      {
                          ProgramName = row.Field<string>(ProgramNameCol),
                          HalfProductCode = row.Field<string>(HalfProductCol),
                          UnitCount = 0

                      }).Distinct();

        List<SMT_ProductBaseInfoModel> list = result.Cast<SMT_ProductBaseInfoModel>().ToList<SMT_ProductBaseInfoModel>();

        if (StartRow != 0)
        {
            list = list.Skip(StartRow - 1).Cast<SMT_ProductBaseInfoModel>().ToList<SMT_ProductBaseInfoModel>();
        }

        if (EndRow != 0)
        {
            list = list.Take(EndRow - StartRow + 1).Cast<SMT_ProductBaseInfoModel>().ToList<SMT_ProductBaseInfoModel>();
        }
        try
        {
            foreach (var item in list)
            {
                _service.InsertOrUpdateForHalfProduct(item);
            }
            ShowMessage("success");
        }
        catch
        {
            ShowMessage("fail");
        }
    }
}